package main.java.indi.zyj.hot100;

import main.java.indi.zyj.hot100.base.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

public class _01_flatten {

    public void flatten(TreeNode root) {

        if (root == null) {
            return;
        }

        Deque<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        TreeNode pre = null;

        while (!stack.isEmpty()) {
            TreeNode curNode = stack.pop();
            if (pre != null) {
                pre.right = curNode;
                pre.left = null;
            }
            if (curNode.right != null) {
                stack.push(curNode.right);
            }
            if (curNode.left != null) {
                stack.push(curNode.left);
            }
            pre = curNode;
        }

    }

}
